package com.example.mydianping.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.mydianping.dto.Result;
import com.example.mydianping.pojo.RedisData;
import com.example.mydianping.pojo.Shop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@Slf4j
@Component
public class CacheClient {

    private StringRedisTemplate stringRedisTemplate;

    //线程池，用于逻辑过期执行更新数据库
    private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10);

    public CacheClient(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    //存入缓存
    public void setForString(String key, Object value, long time, TimeUnit unit){
        String json = JSONUtil.toJsonStr(value);
        stringRedisTemplate.opsForValue().set(key,json,time,unit);
    }
    //删除缓存
    public void removeKey(String key){
        stringRedisTemplate.delete(key);
    }
    //存入缓存，并设置逻辑日期时间
    public void setWithLogicalExpire(String key, Object value, long time, TimeUnit unit){
        RedisData redisData = new RedisData();
        redisData.setData(value);
        redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));//设置过期时间
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData),time,unit);
    }
    //防止穿透的查询店铺信息功能缓存
    public <R,ID> R getById(String preKey, ID id, Class<R> type, Function<ID,R> getById,long time,TimeUnit unit) {
        String key = preKey + id;
        //先在 redis 中查询
        String json = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isNotBlank(json)){
            //(2024-10-11这个逻辑出现了错误，json 并不是 null，可是封装过去后全是 null,原因：用成了 BeanUtil)
            return JSONUtil.toBean(json, type);
        }
        //判断是否穿透后查询到 ""
        if(json != null){
            return null;
        }
        //redis 中没查到，在数据库中查询
        R r = getById.apply(id);
        if(r == null){
            setForString(key,"",time,unit);//防击穿，防雪崩
            return null;
        }
        String s = JSONUtil.toJsonStr(r);
        //(2024-10-11,这一句的逻辑出bug了，找到原因：传参 time 为 0,括号出现了位置错误)
        stringRedisTemplate.opsForValue().set(key,s,time,unit);
        return r;
    }

    //基于互斥锁的方式防止店铺信息查询击穿
    public <R,ID> R queryShopByLock(String preKey, ID id, Class<R> type, Function<ID,R> getById,long time,TimeUnit unit){
        String key = preKey + id;
        //先在 redis 中查询
        String json = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isNotBlank(json)){
            //存在，直接返回
            return JSONUtil.toBean(json, type);
        }
        //判断是否穿透后查询到 ""
        if(json != null){
            return null;
        }
        //redis 中没查到，获取互斥锁
        String lockKey = "shop:lock:"+id;
        R r = null;
        try {
            boolean getLock = tryToGetLock(lockKey);
            if(!getLock){
                //获取锁失败，休眠后重试
                Thread.sleep(50);
                queryShopByLock(preKey,id,type,getById,time,unit);
            }
            //获取锁成功，完成缓存重建
            r = getById.apply(id);
            if(r == null){
                setForString(key,"",time,unit);
                return null;
            }
            String s = JSONUtil.toJsonStr(r);
            stringRedisTemplate.opsForValue().set(key,s,time,unit);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            cleanLock(lockKey);
        }
        return r;
    }

    //基于逻辑过期的方式防止店铺信息查询击穿
    public <R,ID> R queryShopByLogicExpire(String preKey, ID id, Class<R> type , Function<ID,R> getById,long time,TimeUnit unit){
        String key = preKey + id;
        //先在 redis 中查询
        String json = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isBlank(json)){
            //正常情况一定能查到，没查到，返回空对象
            return null;
        }
        //查到了，将 json 转换为可以使用的类
        RedisData redisData = JSONUtil.toBean(json, RedisData.class);
        R r = JSONUtil.toBean((JSONObject) redisData.getData(),type);//将Object类型的json转换为指定 type 的类
        LocalDateTime expireTime = redisData.getExpireTime();
        //判断是否过期，没过期直接返回数据
        if(expireTime.isAfter(LocalDateTime.now())){
            //过期时间在当前时间之后，未过期
            return r;
        }
        //过期了，重建缓存
        String lockKey = "shop:lock:"+id;
        boolean getLock = tryToGetLock(lockKey);
        if(getLock){
            //成功获取锁，再次判断是否过期
            boolean isOverTime = judgeLogicalExpire(key);
            if(!isOverTime){
                //释放锁
                cleanLock(lockKey);
                return r;
            }
            //再次判断依然过期，唤醒处理数据库的线程
            CACHE_REBUILD_EXECUTOR.submit(()->{
                //需要交给线程执行的逻辑
                try {
                    R apply = getById.apply(id);
                    //加上逻辑时间的缓存重建
                    this.setWithLogicalExpire(key,apply,time,unit);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    //释放锁
                    cleanLock(lockKey);
                }
            });
        }
        //返回已过期的数据
        return r;
    }

    //判断是否逻辑过期,true 表示过期
    public boolean judgeLogicalExpire(String key){
        String json = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isBlank(json)){
            return true;
        }
        RedisData redisData = JSONUtil.toBean(json, RedisData.class);
        LocalDateTime expireTime = redisData.getExpireTime();
        return !expireTime.isAfter(LocalDateTime.now());
    }

    //尝试获取锁
    private boolean tryToGetLock(String key){
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "aaa", 10, TimeUnit.SECONDS);
        return BooleanUtil.isTrue(flag);
    }
    //释放锁
    private void cleanLock(String key){
        stringRedisTemplate.delete(key);
    }

}
